package com.example.a2023110508

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import java.util.*

class MainActivity : AppCompatActivity() {
    // 定义变量记录当前语言状态
    private var currentLanguage = 0 // 0: 英文, 1: 中文, 2: 法语

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 设置窗口标题为你的姓名和学号（从 strings.xml 获取）
        setTitle(R.string.app_name) // ✅ 使用资源，不硬编码

        // 1. 创建主布局（垂直方向）
        val mainLayout = LinearLayout(this)
        mainLayout.orientation = LinearLayout.VERTICAL
        mainLayout.gravity = Gravity.CENTER
        mainLayout.layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        )

        // ====================== 区0：原功能 - Hello World + 语言切换 ======================
        val helloText = TextView(this)
        helloText.id = View.generateViewId()
        helloText.text = getString(R.string.hello_world)
        helloText.textSize = 24f
        val textParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        ).apply { setMargins(0, 0, 0, 50) }
        helloText.layoutParams = textParams

        val switchButton = Button(this)
        switchButton.text = getString(R.string.switch_language)
        switchButton.setOnClickListener {
            currentLanguage = (currentLanguage + 1) % 3
            when (currentLanguage) {
                0 -> {
                    helloText.text = getString(R.string.hello_world)
                    switchButton.setBackgroundResource(R.drawable.chinese)
                }
                1 -> {
                    helloText.text = getString(R.string.hello_chinese)
                    switchButton.setBackgroundResource(R.drawable.t01264cf517c674c35c)
                }
                2 -> {
                    helloText.text = getString(R.string.hello_french)
                    switchButton.setBackgroundResource(R.drawable.t01f35bcdab1018a879)
                }
            }
        }

        mainLayout.addView(helloText)
        mainLayout.addView(switchButton)

        // ====================== 区1：动态添加 TextView（带滚动）======================
        val scrollView = ScrollView(this)
        scrollView.layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            800 // 固定高度以便看到滚动效果
        )

        val dynamicLayout = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            gravity = Gravity.CENTER
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
        }

        // 初始添加一条文本
        addTextView(dynamicLayout, R.string.advice_content_1)

        val addButton = Button(this)
        addButton.text = getString(R.string.add_text_button)
        addButton.setOnClickListener {
            val randomRes = listOf(
                R.string.advice_content_1,
                R.string.advice_content_2,
                R.string.advice_content_3,
                R.string.advice_content_4,
                R.string.advice_content_5
            ).random()
            addTextView(dynamicLayout, randomRes)
        }

        scrollView.addView(dynamicLayout)
        mainLayout.addView(scrollView)
        mainLayout.addView(addButton) // 按钮放在 ScrollView 外面

        // ====================== 区2：MVC 模式 - ProgramAdviser ======================
        val adviserButton = Button(this)
        adviserButton.text = getString(R.string.advice_title)
        adviserButton.setOnClickListener {
            val model = ProgramAdviserModel()
            val advice = model.getAdvice(this) // 传入 Context 以获取字符串资源
            Toast.makeText(this, advice, Toast.LENGTH_SHORT).show()
        }
        mainLayout.addView(adviserButton)

        // 最终设置视图为 mainLayout
        setContentView(mainLayout)
    }

    // 辅助方法：动态添加 TextView
    private fun addTextView(container: LinearLayout, textResId: Int) {
        val textView = TextView(this).apply {
            text = getString(textResId)
            textSize = 16f
            setPadding(16, 8, 16, 8)
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
        }
        container.addView(textView)
    }
}

// MVC - Model 层
class ProgramAdviserModel {
    fun getAdvice(context: android.content.Context): String {
        val adviceList = listOf(
            context.getString(R.string.advice_content_1),
            context.getString(R.string.advice_content_2),
            context.getString(R.string.advice_content_3),
            context.getString(R.string.advice_content_4),
            context.getString(R.string.advice_content_5)
        )
        return adviceList[Random().nextInt(adviceList.size)]
    }
}